package com.whccb.apps.office.document.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.LogAPI;
import com.actionsoft.sdk.local.api.Logger;

public class FileUtil {

	private static final Logger logger = LogAPI.getLogger(FileUtil.class);

	public static FormFile getSingleFormFile(String boName, String fieldName, String bindid) {
		FormFile formFile = null;
		if (StringUtils.isNotEmpty(boName) && StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(bindid)) {
			List<FormFile> formFiles = null;
			try {
				formFiles = SDK.getBOAPI().getFiles(SDK.getBOAPI().getByProcess(boName, bindid).getId(), fieldName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (formFiles != null && !formFiles.isEmpty() && formFiles.size() > 0) {
				for (FormFile thisFormFile : formFiles) {
					if (thisFormFile != null) {
						formFile = thisFormFile;
						break;
					}
				}
			}
		}
		return formFile;
	}

	public static List<FormFile> getBoFieldFormFile(String boName, String fieldName, String bindid) throws Exception {
		List<FormFile> formFiles = null;
		if (StringUtils.isNotBlank(boName) && StringUtils.isNotBlank(fieldName) && StringUtils.isNotBlank(bindid)) {
			try {
				formFiles = SDK.getBOAPI().getFiles(SDK.getBOAPI().getByProcess(boName, bindid).getId(), fieldName);
			} catch (Exception e) {
				logger.error("FileUtil中，方法getBoFieldFormFile出现异常！", e);
				throw new Exception();
			}
		}
		return formFiles;
	}

	public static void upFileFromBoFieldToAnotherBoField(String fromBo, String fromField, String fromBindid,
			String toBo, String toField, String toBinid) {
		FormFile formFile = null;
		if (StringUtils.isNotEmpty(fromBo) && StringUtils.isNotEmpty(fromField) && StringUtils.isNotEmpty(fromBindid)) {
			List<FormFile> formFiles = null;
			try {
				formFiles = SDK.getBOAPI().getFiles(SDK.getBOAPI().getByProcess(fromBo, fromBindid).getId(), fromField);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (formFiles != null && !formFiles.isEmpty() && formFiles.size() > 0) {
				for (FormFile thisFormFile : formFiles) {
					if (thisFormFile != null) {
						formFile = thisFormFile;
						break;
					}
				}
			}
		}
		if (formFile != null) {

		}
	}

}
